package cn.tedu.charging.cost.service.impl;

import cn.tedu.charging.common.pojo.param.ChargingProgressParam;
import cn.tedu.charging.common.pojo.vo.ChargingProgressVO;
import cn.tedu.charging.cost.dao.repository.CostRuleRepository;
import cn.tedu.charging.cost.pojo.po.CostRulePO;
import cn.tedu.charging.cost.service.CostService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class CostServiceImpl implements CostService {
    @Autowired
    private CostRuleRepository costRuleRepository;

    @Override
    public ChargingProgressVO doCalcPrice(ChargingProgressParam params) {
        int hour = getHour();
        CostRulePO costRulePO = getMatchCostRuleByStationIdAndTime(params.getStationId(),params.getGunId(),hour);
        ChargingProgressVO chargingProgressVO = new ChargingProgressVO();
        chargingProgressVO.setGunId(params.getGunId());
        return chargingProgressVO;
    }

    private CostRulePO getMatchCostRuleByStationIdAndTime(Integer stationId, Integer gunId, int hour) {
        log.debug("通过场站id:{},获取计价规则列表",stationId);
        List<CostRulePO> costRules = getCostRules(stationId);
        log.debug("通过场站id:{},获取计价规则列表,结果:{}",stationId,costRules);

        for (CostRulePO costRule : costRules) {

            if(hour >= costRule.getStartTime() && hour < costRule.getEndTime()){
                return costRule;
            }
        }
        return null;
    }

    private List<CostRulePO> getCostRules(Integer stationId) {
        List<CostRulePO> list = costRuleRepository.getCostRules(stationId);
        return list;
    }

    private int getHour() {
        return LocalDateTime.now().getHour();
    }
}
